<?php


namespace Index\Controller;
use Think\Controller;
use Think\Requesr;
use Think\Validate;
header("Content-type: text/html; charset=utf-8"); 


class LoginController extends Controller {
    //验证码
    public function verify_c(){  
    $Verify = new \Think\Verify();  
    $Verify->fontSize = 18;  
    $Verify->length   = 4;  
    $Verify->useNoise = false;  
    //$Verify->codeSet = '0123456789';  
    $Verify->imageW = 135;  
    $Verify->imageH = 35;  
    //$Verify->expire = 600;  
    $Verify->entry();  

} 
 /* 验证码校验 */
    public function check_verify()
    {
        $code = $_POST['code'];
        $id = '';
        $verify = new \Think\Verify();
        $res = $verify->check("$code", $id);
        $this->ajaxReturn($res, 'json');
    }
    public function login(){
        if(IS_POST){
        $data = I('post.');

        // 经过logina 验证 , 返回结果
        $status = $this->logina($data['moblie'],$data['pwd']);

        if($status == 1){
           cookie('name',  session('name'));
           echo 1;//登录成功
        }else if($status == 2){
            echo 2;// return $this->error('手机号码或密码错误！');
        }else{
            echo 3;
        }

         return;
        }
      
        $this->display();
    
}


    public function logina($moblie,$pwd){ 
          
        $user = D('users')->where("mobile = '{$moblie}'") -> find();
         // var_dump($user);
         // var_dump($password);
         // var_dump(md5($password));die();
        if($user){
          if($user['password'] == md5($pwd)){
             session('id',$user['user_id']); 
             session('name',$user['user_name']);
             return 1;
          }else{
             return 2;
          }
        }else{
             return 3;
      }
      
}    

    public function register(){
         if(IS_POST){
            $data = I('post.');

            $pw = md5($data['password']);

            $data['password']=$pw;
            $data['reg_time']= time();
        //     $rules = array(
                     
        //     array('user_name','users','该帐号已被注册',0,'unique',1), // 在新增的时候验证name字段是否唯一
                     
        // );    

            $User = M("users");
            $arr = array(
                'user_name' =>  $data['user_name'],
                'reg_time' =>  $data['reg_time'],
                'password' =>  $data['password'],
                'mobile' =>  $data['moblie']
             
            );
            $code = $_SESSION[$data['moblie']]['code'];
            if ( $data['VerifyCode'] == $code){
            // 如果创建失败 表示验证没有通过 输出错误提示信息
           // echo  "成功!验证码正常";
            $add = D('users')->add($arr);
                            if($add){
                             echo 2;
                            }else{
                                $this->error('注册失败，请重试');
                            }
      
            }else{
              echo 1;
                     }
   } else{
            

            

        $this->display('register');
    
    }
}
 
   public function loginout(){
       cookie('name',null);
       return $this->redirect('/');
    
    }
    //判断手机号码
    public function interpretation_mobile(){
        $users = D('users');
        if($_POST){
        $arr =  $users -> where("mobile = '{$_POST['moblie']}'") ->find();
        
        if($arr){
            echo "false";
        }else{
            echo "true";
        }
    }
}
   //判断用户名
    public function interpretation_username(){
        $users = D('users');
        if($_POST){
        $arr =  $users -> where("user_name = '{$_POST['user_name']}'") ->find();
        
        if($arr){
            echo "false";
        }else{
            echo "true";
        }
    }
}

    public function test(){
        var_dump($_SESSION['18028131581']);
    }
    //修改密码
    public function save_paw(){
        $user = D('users');
             if($_POST){
                $pw = md5($_POST['password']);
                $arr = array('password' => $pw);
                
                // var_dump("...........");
                // var_dump($_POST);
                $code = $_SESSION[$_POST['moblie']]['code'];
                 if ($_POST['VerifyCode'] == $code){
                    
                     $id = D('users')-> where("mobile = '{$_POST['moblie']}'") -> save($arr);
                            if($id){
                             echo 2;//修改成功
                            }else{
                            echo 3;//修改失败
                            }              
             }else{
                  echo 1; //验证码错误
             }
         }
    }
    public function forgetPwd(){
         $this->display();
    }
}